#include "DDAS_Researcher_Result_Handler.hpp"

#include <iostream>

using namespace std;

int main()
{
	//result handler
	DDAS_Researcher_Result_Handler result_handler;

	//db handler
	DDAS_DB_Handler db_handler;

	//size of array
	int size;

	//array of completed jobs
	int* completed_jobs;

	//query for any completed jobs
	if( db_handler.getUnprocessedCompleteJobs( &size, &completed_jobs )==0 )
	{
		cout<<"ddas_researcher_result_monitor failed on getUnprocessedCompleteJobs"<<endl;

		//some kind of error
		return 0;
	}

	//if no completed jobs exist return
	if( size<=0 ) return 1;

	//create output files for completed jobs
	for( int i=0; i<size; i++)
	{
		//write output file to html/results
		if( result_handler.writeResultsToXML( completed_jobs[i] )==0 )
		{
			cout<<"ddas_researcher_result_monitor failed on writeResultsToXML"<<endl;

			//some kind of error
			continue;
		}
		

		//mark as complete so it will be excluded from future consideration
		if( db_handler.markJobAsComplete( completed_jobs[i] )==0 )
		{
			cout<<"ddas_researcher_result_monitor failed on markJobAsComplete"<<endl;

			//some kind of error
			continue;
		}
	}

	//free memory
	if(completed_jobs) delete[] completed_jobs;

	//success
	return 1;
}
